package com.thirdgroup.service.impl;

import com.thirdgroup.dao.ManagerInfoDao;
import com.thirdgroup.dao.RbacDao;
import com.thirdgroup.dao.impl.ManagerInfoDaoImpl;
import com.thirdgroup.dao.impl.RbacDaoImpl;
import com.thirdgroup.pojo.entity.CustomerInfo;
import com.thirdgroup.pojo.entity.ManagerInfo;
import com.thirdgroup.pojo.entity.Permission;
import com.thirdgroup.service.ManagerInfoService;
import com.thirdgroup.utils.MD5Util;
import com.thirdgroup.utils.respond.R;

import java.util.List;

public class ManagerInfoServiceImpl implements ManagerInfoService {
    private ManagerInfoDao ManagerInfoDao=new ManagerInfoDaoImpl();
    private RbacDao rbacDao=new RbacDaoImpl();
    @Override
    public R Login(String username,String password) {
        ManagerInfo ManagerInfo = ManagerInfoDao.selectByUsername(username);
        if(ManagerInfo!=null&&!"".equals(ManagerInfo)){
            if(MD5Util.check(password,ManagerInfo.getManagerpassword())){
               return R.ok(ManagerInfo,"登陆成功！");
            }else{
                return  R.error(500,"用户名或密码不正确！！");
            }
        }
        return R.error(500,"用户名或密码不正确！！");
    }
//    管理端权限业务
    public List<Permission> rbac(String id){
        List<Permission> rbac = rbacDao.rbac(id);
        if (rbac != null&& !"".equals(rbac)){
            return rbac;
        }
        return null;
    }

    @Override
    public R selectAll() {
        List<ManagerInfo> managerInfos = ManagerInfoDao.selectAll();
        if (managerInfos != null && !"".equals(managerInfos)){
            return R.ok(managerInfos);
        }
        return R.error(500,"无数据");
    }

    @Override
    public R updateState(String id, String status, String date) {
        Integer integer = ManagerInfoDao.updateState(id, status,date);
        if (integer>=0){
            System.out.println("成功修改"+integer+"条数据！");
            return R.ok("成功修改"+integer+"条数据！");
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }

    @Override
    public R add(String id, String username, String password, String formattedDate, String profilePicture) {
        String encrypt = MD5Util.encrypt(password);
        Integer integer = ManagerInfoDao.add(id, username,encrypt,formattedDate,profilePicture);
        if (integer>=0){
            System.out.println("成功添加"+integer+"条数据！");
            return R.ok("成功添加"+integer+"条数据！");
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }

    @Override
    public R delete(String managerid) {
        Integer integer = ManagerInfoDao.delete(managerid);
        if (integer>=0){
            System.out.println("删除"+integer+"条数据！");
            return R.ok("删除"+integer+"条数据！");
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }

    @Override
    public R detail(String managerid) {
        ManagerInfo integer = ManagerInfoDao.detail(managerid);
        if (integer!=null&&!"".equals(integer)){
            System.out.println("查找到数据！");
            return R.ok(integer);
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }
}
